package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.CategoryMapper;
import com.cskaoyan.catmall.dao.StorageMapper;
import com.cskaoyan.catmall.model.bo.CateGoryCreateBo;
import com.cskaoyan.catmall.model.bo.CateGoryUpdateBo;
import com.cskaoyan.catmall.model.bo.CategoryDeleteBo;
import com.cskaoyan.catmall.model.vo.CategoryL1Vo;
import com.cskaoyan.catmall.model.vo.CategoryListVo;
import com.cskaoyan.catmall.service.CateGoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022-01-07-21-52
 * author : WQW
 * description: 商品类目的增删改查
 */

@Service
public class CateGoryServiceImpl implements CateGoryService {
    private final Integer CATEGORY_PID = 0;
    private final Integer PAGE = 1;
    private final Integer PAGES = 1;

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    StorageMapper storageMapper;

    /**
     * @param
     * @param cateGoryCreateBo
     * @return int
     * @description 添加商品类目
     * @author WQW
     * @date 2022/1/7 21:54
     */
    @Override
    @Transactional
    public int addCateGory(CateGoryCreateBo cateGoryCreateBo) {
        if (cateGoryCreateBo.getPid() == 0) {
            List<String> nameListL1 = categoryMapper.selectNameListL1();
            if (nameListL1.contains(cateGoryCreateBo.getName())) {
                return 404;
            }
        } else {
            List<String> nameListL2 = categoryMapper.selectNameListL2(cateGoryCreateBo.getPid());
            if (nameListL2.contains(cateGoryCreateBo.getName())) {
                return 404;
            }
        }
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String format = dateFormat.format(date);
        cateGoryCreateBo.setAddTime(format);
        cateGoryCreateBo.setUpdateTime(format);
        try {
            categoryMapper.insertCateGory(cateGoryCreateBo);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 404;
        }
        return 200;
    }


    /**
     * @param
     * @param categoryDeleteBo
     * @return int
     * @description
     * @author WQW
     * @date 2022/1/9 22:16
     */
    @Override
    @Transactional
    public int deleteCateGory(CategoryDeleteBo categoryDeleteBo) {
        List<CategoryDeleteBo.ChildrenDTO> children = categoryDeleteBo.getChildren();
//        ArrayList<String> keyList = new ArrayList<>();// 创建ArrayList用于存储所有需要删除的key
//        String picUrl = categoryDeleteBo.getPicUrl();
//        String key1 = picUrl.substring(picUrl.lastIndexOf("/") + 1);
//        String iconUrl = categoryDeleteBo.getIconUrl();
//        String key2 = iconUrl.substring(iconUrl.lastIndexOf("/") + 1);
//        keyList.add(key1);
//        keyList.add(key2);
        try {
            if (children != null && children.size() != 0) {
//                for (CategoryDeleteBo.ChildrenDTO child : children) {
//                    String picUrl1 = child.getPicUrl();
//                    String iconUrl1 = child.getIconUrl();
//                    String key3 = picUrl1.substring(picUrl1.lastIndexOf("/") + 1);
//                    String key4 = iconUrl1.substring(iconUrl1.lastIndexOf("/") + 1);
//                    keyList.add(key3);
//                    keyList.add(key4);
//                }
                categoryMapper.deleteCategoryChildren(children);
            }
            categoryMapper.deleteCategory(categoryDeleteBo);
//            storageMapper.deleteStorageByKey(keyList);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return 404;
        }
        return 200;
    }


    /**
     * @param
     * @param cateGoryUpdateBo
     * @return int
     * @description
     * @author WQW
     * @date 2022/1/9 22:17
     */
    @Override
    @Transactional
    public int updateCateGory(CateGoryUpdateBo cateGoryUpdateBo) {
        if (cateGoryUpdateBo.getChildren() == null) {
            List<String> nameList = categoryMapper.selectNameByPid(cateGoryUpdateBo.getId());
            if (nameList.contains(cateGoryUpdateBo.getName()) || cateGoryUpdateBo.getPid() == null) {
                return 404;
            }
        } else {
            List<String> nameListL1 = categoryMapper.selectNameListL1NoMyName(cateGoryUpdateBo.getId());
            if (nameListL1.contains(cateGoryUpdateBo.getName())) {
                return 404;
            }
        }
        try {
            categoryMapper.updateCateGory(cateGoryUpdateBo);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 404;
        }
        return 200;
    }

    @Override
    public CategoryListVo getCategoryList() {
        List<CategoryListVo.ListDTO> categoryList = categoryMapper.selectCategoryList(CATEGORY_PID);
        return new CategoryListVo(categoryList.size(), PAGES, categoryList.size(), PAGE, categoryList);
    }

    @Override
    public CategoryL1Vo getCategoryL1List() {
        List<CategoryL1Vo.ListDTO> listDTOS = categoryMapper.selectCategoryL1List(CATEGORY_PID);
        return new CategoryL1Vo(listDTOS.size(), PAGES, listDTOS.size(), PAGE, listDTOS);
    }
}
